package com.rentong.config;

import com.rentong.constants.BackConstant;


import com.rentong.filter.RequestInterceptor;
import com.rentong.wx.filter.WxLoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.Resource;

@Configuration
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {

    @Resource
    private SpecialConfig specialConfig;

    @Resource
    private WxPayProperties wxPayProperties;

//    @Bean
//    public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
//        return new OrderedHiddenHttpMethodFilter() {
//            @Override
//            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
//                    throws ServletException, IOException {
//                BufferedReader reader = new BufferedReader(
//                        new InputStreamReader(request.getInputStream(), "UTF-8"));
//                String line;
//                String result = "";
//                while ((line = reader.readLine()) != null) {
//                    result += line;
//                }
//                String url = request.getRequestURL().toString();
//                if(url.contains("back/wx/getMes")){
//                    JSONObject json = JSONObject.fromObject(result.substring(2,result.length()-2));
//                    try{
//                        wxDMonService.getEquipmentState(json);
//                    }catch(Exception e){
//
//                    }
//                }
//                filterChain.doFilter(request, response);
//            }
//        };
//    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
        registry.addResourceHandler("/**")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/");
        registry.addResourceHandler(BackConstant.imgPrefix + "/**")
                .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + specialConfig.getUploadPath());
        registry.addResourceHandler(BackConstant.appPrefix + "/**")
                .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + specialConfig.getAppPath());
        registry.addResourceHandler(BackConstant.certPrefix + "/**")
                .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + wxPayProperties.getCertPath());
        super.addResourceHandlers(registry);
    }

    @Bean
    public RequestInterceptor getRequestInterceptor() {
        return new RequestInterceptor();
    }

    @Bean
    public WxLoginInterceptor getWxLoginInterceptor() {
        return new WxLoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getRequestInterceptor());
        registry.addInterceptor(getWxLoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/wx/pay/notify", "/wx/refund/notify", "/wx/cust/registerByPhone", "/wx/sendCode", "/wx/uploadPicCos");


    }

}
